package com.zcx.base.log.local;

import java.util.UUID;

public class LogThreadLocal {

    private static ThreadLocal<Log> log = new ThreadLocal<Log>();

    public static ThreadLocal<Log> getLog() {
        return log;
    }

    public static void setLog(ThreadLocal<Log> log) {
        LogThreadLocal.log = log;
    }

    public static String getTraceId() {
        Log info = log.get();
        if (info == null) {
            UUID uuid = UUID.randomUUID();
            String uniqueId = "traceId-"+ uuid.toString().replace("-", "");
            info = new Log();
            info.setTraceId(uniqueId);
            log.set(info);
            return uniqueId;
        } else {
            return info.traceId;
        }
    }

    public static void setTraceId(String traceId) {
        Log info = log.get();
        if (info == null) {
            String uniqueId = traceId;
            info = new Log();
            info.setTraceId(uniqueId);
            log.set(info);
        } else {
            info.setTraceId(traceId);
        }
    }
    public static class Log {
        private String traceId;

        public String getTraceId() {
            return traceId;
        }

        public void setTraceId(String traceId) {
            this.traceId = traceId;
        }
    }
}
